package com.mo.web.exception;


import com.mo.web.pojo.JsonResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;

import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 此注解描述的类为全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


       @ExceptionHandler(ShiroException.class)
       public JsonResult doShiroException(ShiroException e){
           e.printStackTrace();
           JsonResult r=new JsonResult();
           r.setState(0);
           if(e instanceof UnknownAccountException){
               r.setMessage("账户不存在");
           }else if(e instanceof IncorrectCredentialsException){
               r.setMessage("密码不正确");
           }
           else if(e instanceof AuthorizationException){
               r.setMessage("没有权限");
           }else{
               r.setMessage("认证或授权失败");
           }
           return r;
       }

}
